<?php
class Ups{
	public $config;
	public $setarr;
	public $getbaseConfig;//获取远程url 配置
	public function __construct(){
		$this->setarr=$_POST;
		$this->up=new Upload();
    	$this->img=new Images();
    	$this->getbaseConfig=include(_BASE_."/data/config.php");
    	$getconfig=file_get_contents($this->getbaseConfig['getConfigUrl']);
    	$this->config=json_decode($getconfig,true);
    	if($this->setarr['uploadtoken']!=md5($this->config['uploadtoken'])){
    		$this->returnError("uploadtoken验证失败");
    	}
	}
	//js 上傳文件跨域问题
	public function return_js($return){
		$url=$this->getbaseConfig['setDomainUrl'].urlencode($return);
		echo Js::to_url($url);
	}
	//判断是否存在方法
	public function __call($method,$args=array()){
		$result['status']=false;
		$result['msg']=$method."方法不存在";
		$json=json_encode($result);
		$this->return_js($json);
	}
	//参数错误
	public function returnError($msg='参数错误'){
		$result['status']=false;
		$result['msg']=$msg;
		$json=json_encode($result);
		$this->return_js($json);
	}
	//只上传文件返回
	private function up_return($info){
		//返回回调函数
		$return=$this->setarr;
		if(!$info){
			$return['status']=false;
			$return['msg']=$this->up->getError();
		}else{
			$return['status']=true;
			$return['msg']="上传成功";
			$return['imgshow']=$this->config['upload_http'].trim($info[0],'./');
			$return['img']=trim($info[0],'./');
		}
		$return=json_encode($return);
		$this->return_js($return);
	}
	//上传文件和缩略图返回
	private function thumb_return($info){
		//返回回调函数
		$return=$this->setarr;
		if(!$info['thumbimg']){
			$return['status']=false;
			$return['msg']=$this->img->getError();
		}else{
			//是否删除原图
			if($upinfolist['imgdel']==1){unlink($info[0]);}
			$return['status']=true;
			$return['msg']="上传成功";
			$return['imgshow']=$this->config['upload_http'].trim($info['thumbimg'],'./');
			$return['img']=trim($info['thumbimg'],'./');
		}
		$return=json_encode($return);
		$this->return_js($return);
	}
	//上传水印返回
	private function water_return($info){
		//返回回调函数
		$return=$this->setarr;
		if(!$info['waterimg']){
			$return['status']=false;
			$return['msg']=$this->img->getError();
		}else{
			//是否删除原图
			if($upinfolist['imgdel']==1){unlink($info[0]);}
			$return['status']=true;
			$return['msg']="上传成功";
			$return['imgshow']=$this->config['upload_http'].trim($info['waterimg'],'./');
			$return['img']=trim($info['waterimg'],'./');
		}
		$return=json_encode($return);
		$this->return_js($return);
	}
	//上传站点logo
	public function article(){
		$setconfig[]=array('savePath',$this->setarr['classname']);
		$exts=explode(',',$this->config['exts']);
		$setconfig[]=array('exts',$exts);
		$this->up->setconfig($setconfig);
		$files=$_FILES['upfileone'];
		$info=$this->up->upload_one($files);
		if(!$info){$this->up_return($info);}
		if($this->config['thumb']==0 && $this->config['water']==0){//不生产缩略图 和 不生产水印
			$this->up_return($info);
		}elseif($this->config['water']==0 && $this->config['thumb']==1){//生成缩略图 和 不生产水印
			$thumbimg=$this->img->thumb($info[0],$this->config['thumbWidth'],$this->config['thumbHeight']);
			$info['thumbimg']=$thumbimg;
			$this->thumb_return($info);
		}elseif($this->config['thumb']==0 && $this->config['water']==1){//生成水印，但是不生产缩略图
			if($this->config['waterType']==0){
				//水印图片处理
				$waterimg='./'.$this->config['waterImg'];
				$info['waterimg']=$this->img->imgWater($info[0],$waterimg,$this->config['waterMarkpos']);
			}elseif($this->config['waterType']==1){
				$fonttype='./data/fonts/'.$this->config['waterFont'];
				$info['waterimg']=$this->img->textWater($info[0],$this->config['waterTXT'],$this->config['waterColor'],$this->config['waterMarkpos'],$this->config['waterFontSize'],$fonttype);
			}
			$this->water_return($info);
		}elseif($this->config['water']==1 && $this->config['thumb']==1){//生成缩略图和 生成水印

			$thumb_img=$this->img->thumb($info[0],$this->config['thumbWidth'],$this->config['thumbHeight']);
			if(!$thumb_img){
				$return['msg']=$this->img->getError();
				$resreturn=json_encode($return);
				$this->return_js($resreturn);
				exit;
			}
			if($this->config['waterType']==0){
				//水印图片处理
				$waterimg='./'.$this->config['waterImg'];
				$info['waterimg']=$this->img->imgWater($thumb_img,$waterimg,$this->config['waterMarkpos']);
			}elseif($this->config['waterType']==1){
				$fonttype='./data/fonts/'.$this->config['waterFont'];
				$info['waterimg']=$this->img->textWater($thumb_img,$this->config['waterTXT'],$this->config['waterColor'],$this->config['waterMarkpos'],$this->config['waterFontSize'],$fonttype);
			}
			$this->water_return($info);
		}
	}
	//上传站点logo
	public function sites(){
		$setconfig[]=array('savePath',$this->setarr['classname']);
		$exts=explode(',',$this->config['exts']);
		$setconfig[]=array('exts',$exts);
		$this->up->setconfig($setconfig);
		$files=$_FILES['upfileone'];
		$info=$this->up->upload_one($files);
		$this->up_return($info);
	}
	//上传水印图片
	public function water(){
		$setconfig[]=array('savePath',$this->setarr['classname']);
		$exts=explode(',',$this->config['exts']);
		$setconfig[]=array('exts',$exts);
		$this->up->setconfig($setconfig);
		$files=$_FILES['upfileone'];
		$info=$this->up->upload_one($files);
		$this->up_return($info);
	}
	//上传栏目图片
	public function column(){
		$setconfig[]=array('savePath',$this->setarr['classname']);
		$exts=explode(',',$this->config['exts']);
		$setconfig[]=array('exts',$exts);
		$this->up->setconfig($setconfig);
		$files=$_FILES['upfileone'];
		$info=$this->up->upload_one($files);
		$this->up_return($info);
	}
	//上传专题图片
	public function special(){
		$setconfig[]=array('savePath',$this->setarr['classname']);
		$exts=explode(',',$this->config['exts']);
		$setconfig[]=array('exts',$exts);
		$this->up->setconfig($setconfig);
		$files=$_FILES['upfileone'];
		$info=$this->up->upload_one($files);
		$this->up_return($info);
	}
	//上传专题背景图片
	public function specialbg(){
		$setconfig[]=array('savePath',$this->setarr['classname']);
		$exts=explode(',',$this->config['exts']);
		$setconfig[]=array('exts',$exts);
		$this->up->setconfig($setconfig);
		$files=$_FILES['upfileone'];
		$info=$this->up->upload_one($files);
		$this->up_return($info);
	}
	//上传管理员图片
	public function adminuserface(){
		$setconfig[]=array('savePath',$this->setarr['classname']);
		$exts=explode(',',$this->config['exts']);
		$setconfig[]=array('exts',$exts);
		$this->up->setconfig($setconfig);
		$files=$_FILES['upfileone'];
		$info=$this->up->upload_one($files);
		$this->up_return($info);
	}
	//上传管理员图片
	public function userinfoface(){
		$setconfig[]=array('savePath',$this->setarr['classname']);
		$exts=explode(',',$this->config['exts']);
		$setconfig[]=array('exts',$exts);
		$this->up->setconfig($setconfig);
		$files=$_FILES['upfileone'];
		$info=$this->up->upload_one($files);
		$this->up_return($info);
	}
	//上传管理员图片
	public function linklogo(){
		$setconfig[]=array('savePath',$this->setarr['classname']);
		$exts=explode(',',$this->config['exts']);
		$setconfig[]=array('exts',$exts);
		$this->up->setconfig($setconfig);
		$files=$_FILES['upfileone'];
		$info=$this->up->upload_one($files);
		$this->up_return($info);
	}
}
?>